package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: dw
 * @Date 2022/3/16 19:00
 */
@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐管理接口分类")
public class SetmealController {

    @Autowired(required = false)
    private SetmealService setmealService;


    /**
     * 分页条件模糊查询
     * @param page 当前页
     * @param pageSize 每页数据
     * @param name 条件
     * @return
     */
    @RequestMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R<Page<SetmealDto>> findByPage(Integer page,Integer pageSize,String name){
        Page<SetmealDto> setmealDtoPage = setmealService.findAllByPage(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @ApiOperation(value = "新增套餐接口")
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.save(setmealDto);

        return R.success("添加套餐成功");
    }

    /**
     * 批量修改状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam("ids") List<Long> ids){
        setmealService.updateStatus(status,ids);
        return R.success("修改成功");
    }


    /**
     * 根据id回显页面
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    public R<SetmealDto> findBySetmealId(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.findBySetmealId(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改成功");
    }


    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "套餐删除接口")
    public R<String> deleteSetmeals(@RequestParam("ids") List<Long> ids){
        setmealService.deleteSetmeals(ids);
        return R.success("删除成功");
    }


    /**
     * 根据套餐id展示所有菜品
     * @param categoryId
     * @return
     */

    @RequestMapping("/list")
    public R<List<SetmealDto>> findCategoryIdAndStatus(Long categoryId, Integer status){
        List<SetmealDto> setmealDtoList = setmealService.findCategoryIdAndStatus(categoryId, status);
        return R.success(setmealDtoList);
    }


}
